package com.cabrida.livestar.message;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.model.FansWelfareData;

/**
 *  福利adapter
 * 
 * @author why
 * 
 */
public class WelfareAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<FansWelfareData> data_list = new ArrayList<FansWelfareData>();
	private static final int TYPE1 = 10;
    private static final int TYPE2 = 20;
    private int type = TYPE1;
    private int VIEW_TYPE = 30;// getViewTypeCount 返回的数量 要比用到的type类型大

	public WelfareAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getItemViewType(int position) {
		if (String.valueOf(TYPE1).equals(data_list.get(position).getO_type())) {
			type = TYPE1;
		}else if (String.valueOf(TYPE2).equals(data_list.get(position).getO_type())) {
			type = TYPE2;
		}
		return type;
	}
	
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return VIEW_TYPE;
	}

	public void setData(ArrayList<FansWelfareData> data) {
		data_list = data;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		ViewHolder1 holder1 = null;
		if (convertView == null) {
			if (type == TYPE1) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.adapter_welfare_redpacket_lv_item, parent,false);
				holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
				holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				holder.tv_des = (TextView) convertView.findViewById(R.id.tv_des);
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
				convertView.setTag(holder);
			} else if (type == TYPE2) {
				holder1 = new ViewHolder1();
				convertView = mInflater.inflate(R.layout.adapter_welfare_gift_lv_item,  parent,false);
				holder1.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
				holder1.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				holder1.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
				holder1.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
				holder1.tv_pwd = (TextView) convertView.findViewById(R.id.tv_pwd);
				holder1.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder1.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
				holder1.tv_validtime = (TextView) convertView.findViewById(R.id.tv_validtime);
				holder1.tv_merchant = (TextView) convertView.findViewById(R.id.tv_merchant);
				holder1.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel);
				holder1.tv_web = (TextView) convertView.findViewById(R.id.tv_web);
				holder1.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
				convertView.setTag(holder1);
			}

		} else {
			if (type == TYPE1) {
				holder = (ViewHolder) convertView.getTag();
			} else if (type == TYPE2) {
				holder1 = (ViewHolder1) convertView.getTag();
			}

		}
		
		if (type == TYPE1) {
			holder.tv_time.setText(data_list.get(position).getO_createtime());
			holder.tv_title.setText(data_list.get(position).getG_sponsor()+"元红包");
			holder.tv_des.setText("已折换为"+data_list.get(position).getG_sponsor()+"L币，并存入你的账户");
			holder.tv_name.setText(data_list.get(position).getS_name());
			holder.tv_date.setText(data_list.get(position).getO_createtime());
		} else if (type == TYPE2) {
			holder1.tv_time.setText(data_list.get(position).getO_createtime());
			holder1.tv_title.setText(data_list.get(position).getG_name());
			holder1.tv_number.setText(data_list.get(position).getG_number());
			holder1.tv_pwd.setText(data_list.get(position).getG_password());
			holder1.tv_name.setText(data_list.get(position).getS_name());
			holder1.tv_date.setText(data_list.get(position).getO_createtime());
			holder1.tv_validtime.setText(data_list.get(position).getG_validtime());
			holder1.tv_merchant.setText(data_list.get(position).getG_sponsor());
			holder1.tv_tel.setText(data_list.get(position).getG_phone());
			holder1.tv_web.setText(data_list.get(position).getG_url());
			holder1.tv_description.setText(data_list.get(position).getG_description());
			if ("30".equals(data_list.get(position).getG_status())) {
				holder1.tv_state.setText("未领取");
			}else {
				holder1.tv_state.setText("已领取");
			}
		}

		return convertView;
	}

	class ViewHolder {
		private TextView tv_time;
		private TextView tv_title;
		private TextView tv_des;
		private TextView tv_name;
		private TextView tv_date;
	}
	class ViewHolder1 {
		private TextView tv_time;
		private TextView tv_title;
		private TextView tv_state;
		private TextView tv_number;
		private TextView tv_pwd;
		private TextView tv_name;
		private TextView tv_date;
		private TextView tv_validtime;
		private TextView tv_merchant;
		private TextView tv_tel;
		private TextView tv_web;
		private TextView tv_description;
	}

}
